using System;
using System.Collections.Generic;

using Castle.Services.Transaction;

using OrderSystem.Core.Dao;
using OrderSystem.Core.Domain;
using OrderSystem.Core.Manager;

namespace OrderSystem.Core.Manager.Impl
{
    [Transactional]
    public class BaseEntityManager<T> : IBaseEntityManager<T> where T : BaseEntity
    {
        protected IBaseDao<T> baseDao;

        public BaseEntityManager(IBaseDao<T> baseDao)
        {
            this.baseDao = baseDao;
        }

        #region IBaseEntityManager<T> Members
        [Transaction(TransactionMode.Requires)]
        public T GetById(int id)
        {
            return baseDao.GetById(id);
        }

        [Transaction(TransactionMode.Requires)]
        public T Get(int id)
        {
            return baseDao.GetById(id);
        }


        [Transaction(TransactionMode.Requires)]
        public virtual T Save(T instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance", "Domain entity for saving must not be null.");
            }

            baseDao.Save(instance);

            return instance;
        }

        [Transaction(TransactionMode.Requires)]
        public virtual void Delete(T instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance", "Entity refernce cann't be null.");
            }
            baseDao.Delete(instance);
        }

        [Transaction(TransactionMode.Requires)]
        public IList<T> GetAll()
        {
            return baseDao.GetAll();
        }
        #endregion
    }
}